package util

type PageResponse struct {
	Total int64 `json:"total"`
	Page  int64 `json:"page"`
	Size  int64 `json:"size"`
	Count int64 `json:"count"`
	List  any   `json:"list"`
}

type PageRequest struct {
	Page  int64 `json:"page"`
	Size  int64 `json:"size"`
	Total int64 `json:"total"`
	Modal any   `json:"modal"`
}

// GetOffset 获取分页偏移量
func (p *PageRequest) GetOffset() int64 {
	if p.Page <= 0 {
		p.Page = 1
	}
	if p.Size <= 0 {
		p.Size = 10
	}
	return (p.Page - 1) * p.Size
}

// GetLimit 获取分页大小
func (p *PageRequest) GetLimit() int64 {
	if p.Size <= 0 {
		return 10
	}
	return p.Size
}

// BuildPageResponse 构建分页响应
func BuildPageResponse(req *PageRequest, list any, total int64) *PageResponse {
	if req.Page <= 0 {
		req.Page = 1
	}
	if req.Size <= 0 {
		req.Size = 10
	}

	count := total / req.Size
	if total%req.Size > 0 {
		count++
	}

	return &PageResponse{
		Total: total,
		Page:  req.Page,
		Size:  req.Size,
		Count: count,
		List:  list,
	}
}
